﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyCommerce.Presentation.Orders.IViews;
using Innova.Microfinanzas.BusinessEntities;
using MyCommerce.Business.GeneralParameters;
using Innova.Microfinanzas.BusinessEntities.EnumClassifierTypes;
using MyCommerce.Business.Stores;
using Innova.FoundationFramework.DataTypes;
using System.Data;
using Innova.Microfinanzas.BusinessEntities.EnumClassifiers;
using System.Data.Objects.DataClasses;

namespace MyCommerce.Presentation.Orders.Presenters
{
    public class NewOrderToSupplierPresenter
    {
        private INewOrderToSupplierView pViewToPresent;

        public NewOrderToSupplierPresenter(INewOrderToSupplierView vViewToPresent)
        {
            pViewToPresent = vViewToPresent;
            pViewToPresent.OnAddDetail += new EventHandler(pViewToPresent_OnAddDetail);
            pViewToPresent.OnAddRemoveConveyor += pViewToPresent_OnAddRemoveConveyor;
            pViewToPresent.OnCancel += new EventHandler(pViewToPresent_OnCancel);
            pViewToPresent.OnRemoveDetail += new EventHandler(pViewToPresent_OnRemoveDetail);
            pViewToPresent.OnSave += new EventHandler(pViewToPresent_OnSave);
            pViewToPresent.OnSaveDelivery += new EventHandler(pViewToPresent_OnSaveDelivery);
            pViewToPresent.OnLoadView += new EventHandler(pViewToPresent_OnLoadView);
            pViewToPresent.OnLoadOrder += new EventHandler(pViewToPresent_OnLoadOrder);
        }

        private void pViewToPresent_OnSaveDelivery(object sender, EventArgs e)
        {
            OrderSupplierManager orderMgr = new OrderSupplierManager(true);
            orderMgr.SaveOrderDelivery(pViewToPresent.IdSelectedOrder, pViewToPresent.DTOrderDetail);
            pViewToPresent.CloseAndSetResult(true);
        }

        private void pViewToPresent_OnLoadOrder(object sender, EventArgs e)
        {
            if (pViewToPresent.IdSelectedOrder != 0)
            {
                OrderSupplierManager orderMgr = new OrderSupplierManager(true);
                pViewToPresent.LoadOrderSupplier(orderMgr.GetOrdersById(pViewToPresent.IdSelectedOrder));
            }
        }

        private void pViewToPresent_OnLoadView(object sender, EventArgs e)
        {
            CommonClassifierManager classMgr = new CommonClassifierManager();
            pViewToPresent.ColMoneys = classMgr.GetClassifierByIDType(Convert.ToInt64(CommonClassifierTypes.CurrencyClassfier));
            ConveyorManager conveyorMgr = new ConveyorManager();
            pViewToPresent.ColConveyors = conveyorMgr.GetConveyors();
            pViewToPresent.ColSuppliers = new SupplierManager().GetSuppliers();
        }

        private void pViewToPresent_OnSave(object sender, EventArgs e)
        {
            OrderSupplierManager orderMgr = new OrderSupplierManager(true);
            orderMgr.CreateOrderSupplier(pViewToPresent.ColSelectedConveyors, pViewToPresent.IdSelectedStore, pViewToPresent.IdUser, pViewToPresent.IdSupplier, pViewToPresent.IdMoney, pViewToPresent.DTOrderDetail);
            pViewToPresent.CloseAndSetResult(true);
        }

        private void pViewToPresent_OnRemoveDetail(object sender, EventArgs e)
        {
            pViewToPresent.OnAddRemoveNewDetail(null, false);
            if (pViewToPresent.DTOrderDetail == null
                || pViewToPresent.DTOrderDetail.Rows.Count == 0)
                pViewToPresent.EnableOrderData(true);
        }

        private void pViewToPresent_OnCancel(object sender, EventArgs e)
        {
            pViewToPresent.CloseAndSetResult(false);
        }

        private void pViewToPresent_OnAddRemoveConveyor(object sender, GenericEventArgs<long> e)
        {
            pViewToPresent.AddRemoveConveyor(e.EventData);
        }

        private void pViewToPresent_OnAddDetail(object sender, EventArgs e)
        {            
            ProductNewOrderDetail objNewDetail = pViewToPresent.InvokeNewDetail(pViewToPresent.IdSupplier, pViewToPresent.IdMoney);
            if (objNewDetail != null)
            {
                pViewToPresent.OnAddRemoveNewDetail(objNewDetail, true);
                pViewToPresent.EnableOrderData(false);
            }
        }
    }
}
